import { domg } from "../js/init.js";

<<<<<<< HEAD
class Alert {
=======
export default class Alert {
>>>>>>> 758185efc9ae7ad1fbf8d5ab9198f268705acd49
  public props: Array<string>;
  private timeNum: number = 2000;
  constructor(props: props, time?: number, ele: HTMLElement = document.body) {
    if (time) {
      this.timeNum = time;
    }
    this.props = this.registry(props);
    this.create.bind(this)(ele);
    this.time.bind(this)(ele);
  }
  //根据参数数量来规定格式
  registry(p: props): Array<string> {
    if (typeof p === "string") {
      const arr = ["提示", p, "确认"];
      return arr;
    }
    return p;
  }
  //创建提示实例
  create(ele: HTMLElement) {
    const str = `
    <div id='alertCSS'>
      <div class='title'>
        ${this.props[0]}
      </div>
      <span class='content'>${this.props[1]}</span>
      <div class='btn'>${this.props[2] ? this.props[2] : "确认"}</div>
    </div>`;
    ele.insertAdjacentHTML("beforeend", str);
    domg("#alertCSS>.btn").onclick = this.active;
  }
  //单击
  active(e: Event) {
    const alert = (this as unknown as HTMLElement).parentElement;
    document.body.removeChild(alert as HTMLElement);
  }
  //自动消失时间
  time(e: HTMLElement) {
    const fir = e.children[e.children.length - 1];
    setTimeout(() => {
      if (e) {
        try {
          e.removeChild(fir);
        } catch (error) {}
      }
    }, this.timeNum);
  }
}
<<<<<<< HEAD
// export default Alert;
=======

>>>>>>> 758185efc9ae7ad1fbf8d5ab9198f268705acd49
type props = Array<string> | string;
